<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:zj="http://www.zju.edu.cn" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
  <xsl:import href="../Tk2Base/TransformData.xslt"/>
  <xsl:template match="/">
    <xsl:variable name="zj:AllXmlData">
      <xsl:apply-templates select="Toolkit/zj:Module/zj:Resolvers/zj:Resolver[(@Style = 'Normal' and (not(@Type) or @Type != 'View')) or (@DetailList and @DetailList = 'true')]" mode="zj:RawData"/>
    </xsl:variable>
    <xsl:variable name="zj:AllXmlNodeSet" select="msxsl:node-set($zj:AllXmlData)/zj:Table"/>
    <Toolkit xmlns:zj="http://www.zju.edu.cn" Desc="{Toolkit/zj:Module/zj:Title}">
      <xsl:call-template name="ModuleNode"/>
      <xsl:apply-templates select="$zj:AllXmlNodeSet" mode="zj:Data"/>
    </Toolkit>
  </xsl:template>
  <xsl:template name="PaserXml">
    <xsl:param name="nodes"></xsl:param>
    <xsl:param name ="doc"></xsl:param>
    <xsl:variable name="TableName">
      <xsl:choose>
        <xsl:when test="@TableName">
          <xsl:value-of select="@TableName"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="$doc/@TableName"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <xsl:variable name="TableDesc">
      <xsl:choose>
        <xsl:when test="@TableDesc">
          <xsl:value-of select="@TableDesc"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="$doc/@TableDesc"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <xsl:variable name="Main">
      <xsl:choose>
        <xsl:when test="@Main">
          <xsl:value-of select="@Main"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:variable name="Count" select="count(ancestor::zj:Resolvers/zj:Resolver[(@Style = 'Normal') and (not(@Type) or @Type != 'View')])"/>
          <xsl:if test="$Count=1">true</xsl:if>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <xsl:variable name="NewTable">
      <zj:Table TableName="{$TableName}" TableDesc="{$TableDesc}" Style="Normal" Main="{$Main}" SrcTableName="{$doc/@TableName}" KeyCount="{count($doc/zj:Field[boolean(@IsKey='true')])}" RegName="{zj:RegName}" XmlFile="{zj:Xml}">
        <xsl:if test="@DetailList='true'">
          <xsl:if test="@Source">
            <xsl:attribute name="Source"><xsl:value-of select="@Source"/></xsl:attribute>
          </xsl:if>
          <xsl:attribute name="Style">Table</xsl:attribute>
        </xsl:if>
        <xsl:attribute name="Expand"><xsl:choose><xsl:when test="@Expand='false'">false</xsl:when><xsl:otherwise>true</xsl:otherwise></xsl:choose></xsl:attribute>
        <xsl:call-template name="zj:SetNewTableData">
          <xsl:with-param name="ResolverData" select="."/>
          <xsl:with-param name="TableData" select="$doc"/>
        </xsl:call-template>
        <xsl:copy-of select="zj:ListRefParams"/>
      </zj:Table>
    </xsl:variable>
    <xsl:apply-templates select="msxsl:node-set($NewTable)/zj:Table" mode="zj:RawData"/>
  </xsl:template>
  <xsl:template name="zj:SetNewTableData">
    <xsl:param name="ResolverData"/>
    <xsl:param name="TableData"/>
    <xsl:choose>
      <xsl:when test="$ResolverData/@DetailList='true'">
        <xsl:variable name="OverrideInfo" select="$ResolverData/zj:ListAction"/>
        <xsl:variable name="OverrideFieldName" select="$OverrideInfo/Override/Field/@FieldName"/>
        <xsl:variable name="Fields" select="$TableData/zj:Field[zj:FieldName = $OverrideFieldName or zj:Display/@DefaultShow='All' or contains(zj:Display/@DefaultShow, 'List')]"/>
        <xsl:call-template name="zj:AddRawField">
          <xsl:with-param name="OverrideInfo" select="$OverrideInfo"/>
        </xsl:call-template>
        <xsl:apply-templates select="$Fields" mode="zj:RawData">
          <xsl:with-param name="OverrideInfo" select="$OverrideInfo"/>
          <xsl:with-param name="Fields" select="$Fields"/>
        </xsl:apply-templates>
      </xsl:when>
      <xsl:otherwise>
        <xsl:variable name="OverrideInfo" select="$ResolverData/zj:DetailAction"/>
        <xsl:variable name="OverrideFieldName" select="$OverrideInfo/Override/Field/@FieldName"/>
        <xsl:variable name="Fields" select="$TableData/zj:Field[zj:FieldName = $OverrideFieldName or zj:Display/@DefaultShow='All' or zj:Display/@DefaultShow='AllNoList' or contains(zj:Display/@DefaultShow, 'Detail')]"/>
        <xsl:call-template name="zj:AddRawField">
          <xsl:with-param name="OverrideInfo" select="$OverrideInfo"/>
        </xsl:call-template>
        <xsl:apply-templates select="$Fields" mode="zj:RawData">
          <xsl:with-param name="OverrideInfo" select="$OverrideInfo"/>
          <xsl:with-param name="Fields" select="$Fields"/>
        </xsl:apply-templates>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  <xsl:template match="zj:Field" mode="zj:HtmlCtrl">
    <xsl:param name="HtmlCtrl"/>
    <xsl:param name="Style"/>
    <xsl:choose>
      <xsl:when test="$HtmlCtrl='Hidden'">Hidden</xsl:when>
      <xsl:when test="$HtmlCtrl='CheckBoxList' or $HtmlCtrl='MultiSelect' or $HtmlCtrl='DetailMultiSelect'">DetailMultiSelect</xsl:when>
      <xsl:when test="$HtmlCtrl='HTML' or $HtmlCtrl='DetailHTML'">DetailHTML</xsl:when>
      <xsl:when test="$HtmlCtrl='Upload'">DetailUpload</xsl:when>
      <xsl:when test="$HtmlCtrl='DetailHidden'">DetailHidden</xsl:when>
      <xsl:when test="$HtmlCtrl='DetailTextArea'">DetailTextArea</xsl:when>
      <xsl:otherwise>Detail</xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  <xsl:template name="zj:ListRefParams">
    <xsl:param name="Resolver"/>
    <xsl:param name="Relation"/>
    <xsl:param name="Tables"/>
    <xsl:for-each select="$Relation">
      <xsl:variable name="MasterResolver" select="zj:MasterResolver"/>
      <zj:ListRefParams Name="{zj:DetailField}" Insert="Only" FieldName="{zj:MasterField}" TableName="{$Tables[@RegName=$MasterResolver]/@TableName}"/>
    </xsl:for-each>
  </xsl:template>
  <xsl:template match="zj:Field" mode="zj:RawData">
    <xsl:param name="OverrideInfo"/>
    <xsl:param name="Fields"/>
    <xsl:variable name="FieldName" select="zj:FieldName"/>
    <xsl:choose>
      <xsl:when test="$OverrideInfo/Except/DelField[@FieldName=$FieldName]"/>
      <xsl:when test="$Fields/zj:Upload/@ContentField=$FieldName"/>
      <xsl:otherwise>
        <xsl:call-template name="DefaultRavData">
          <xsl:with-param name="OverrideInfo" select="$OverrideInfo"/>
          <xsl:with-param name="Fields" select="$Fields"/>
          <xsl:with-param name="FieldName" select="$FieldName"/>
        </xsl:call-template>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
</xsl:stylesheet>
